#include "libORCL/ORCL.h"
#include "libORCL/ORCL_size.h"
#include <getopt.h>

int main(int argc,char** argv){

    // 服务器编号
    int server_num = -1;

    static struct option opts[] = {
        {"server_num", required_argument, 0, 'n'},
    };

    while (1){
        int c = getopt_long(argc, argv, "n:", opts, NULL);

        if (c == -1)
        {
            break;
        }

        switch (c){
            case 'n':
                server_num = atoi(optarg);
                break;
            
            default:
                printf("Invalid argument %d\n", c);
                assert(false);
        }
    }

    assert(server_num>=0);

    int port_index = 0;
    int numa_id = 0;

    // 0号在一张卡上，其他的在另一张卡上
    if(server_num > 0){
        port_index = 1;
        numa_id = 0;
    }else{
        port_index = 0;
        numa_id = 1;
    }

    // 每组的数量
    int group_size = 1;
    // 当前组号，0、1在0组，2、3在1组
    int group_id = server_num / group_size;

    // 下面进行连接
    // 测试一下初始化
    struct orcl_context* orcl_ctx = orcl_context_init(server_num,group_id,4,4,group_size,2,port_index,numa_id,NULL,M_2,16*server_num+1,NULL,0);

    // 初始化一下缓冲区
    memset((void *)orcl_ctx->message_buffer, 0, orcl_ctx->message_buffer_size);

    if(server_num == 0){
        // 向123分别发送
        orcl_dci_rdma_write(orcl_ctx, 1, 0, 0, 64);
        orcl_dci_rdma_write(orcl_ctx, 2, 0, 0, 64);
        orcl_dci_rdma_write(orcl_ctx, 2, 0, 0, 64);

        sleep(1);

        orcl_publish_ready("finish0");

    }else{
        orcl_wait_till_ready("finish0");
    }
    
    

    return 1;
}